/* 
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.sample.server.dao;

import com.avdy.p4j.common.DbmsType;
import com.avdy.p4j.jdbc.service.DataProvider;
import com.avdy.p4j.jdbc.service.GenericDAO;
import com.avdy.p4j.jdbc.service.GenericDaoImpl;
import com.avdy.p4j.jdbc.service.TransferUtil;
import com.avdy.p4j.sample.server.dao.derby.DerbySampleDataRepository;
import com.avdy.p4j.sample.server.dao.mysql.MySqlSampleDataRepositoryImpl;
import com.avdy.p4j.sample.server.dao.pgsql.PgSqlSampleDataRepositoryImpl;
import com.avdy.p4j.sample.shared.data.Author;
import com.avdy.p4j.sample.shared.data.Book;
import com.avdy.p4j.sample.shared.data.BookInventory;
import com.avdy.p4j.sample.shared.data.BookIssue;
import com.avdy.p4j.sample.shared.data.Student;

public abstract class LibraryDataRepository 
{
	//
	public abstract StudentRepository getStudentRepository();	
	//
	public abstract BookRepository getBookRepository();
	//
	protected DataProvider dataProvider;
		
	/**
	 * 
	 * @param dataFetcher DataFetcher
	 * 
	 * @throws Exception
	 */
	public LibraryDataRepository(DataProvider dataProvider) throws Exception
	{
		this.dataProvider = dataProvider;
		TransferUtil.registerClass(Author.class);
		TransferUtil.registerClass(Book.class);
		TransferUtil.registerClass(Student.class);
		TransferUtil.registerClass(BookIssue.class);
		TransferUtil.registerClass(BookInventory.class);
	}
	
	
	/**
	 * 
	 * @param dataFetcher DataFetcher
	 * 
	 * @return SampleDataRepository
	 * 
	 * @throws Exception
	 */
	public static LibraryDataRepository getLibraryDataRepository(DataProvider dataProvider) throws Exception
	{
		String dbmsName = dataProvider.getDbms();
		if(dbmsName.equalsIgnoreCase(DbmsType.MYSQL))
			return new MySqlSampleDataRepositoryImpl(dataProvider);
		else if(dbmsName.equalsIgnoreCase(DbmsType.PGSQL))
			return new PgSqlSampleDataRepositoryImpl(dataProvider);
		else if(dbmsName.equalsIgnoreCase(DbmsType.DERBY))
			return new DerbySampleDataRepository(dataProvider);
		else 
			throw new RuntimeException("Implementation for this repository is not found");
	}
	
	
	/**
	 * 
	 * @return GenericDaoImpl
	 */
	public <E> GenericDAO<E> getGenericDaoImpl()
	{
		return new GenericDaoImpl<E>(dataProvider.getDataFetcher());
	}
	
	/**
	 * 
	 * @return DataProvider
	 */
	public DataProvider getDataProvider()
	{
		return dataProvider;
	}
}
